﻿Imports System.Activities
Imports System.ComponentModel
Imports System.Activities.Presentation.PropertyEditing
Imports System.Activities.Presentation.Metadata
Imports System.Drawing

Public Class OutputFileActivity
    Inherits AsyncCodeActivity
    Implements IRegisterMetadata

    Public Property InStrings As InArgument(Of String)
    Public Property OutputFile As InArgument(Of String)
    Public Property Append As InArgument(Of Boolean)
    Public Property Result As OutArgument(Of Boolean)

    Private Delegate Function asyncOutputFileDelegate(ByVal target As String, ByVal value As Object, ByVal append As Boolean) As Boolean

    Public Sub New()
        Me.DisplayName = My.Resources.OutputFileDisplayName
    End Sub

    Protected Overrides Function BeginExecute(context As System.Activities.AsyncCodeActivityContext, callback As System.AsyncCallback, state As Object) As System.IAsyncResult
        Dim outFile = context.GetValue(Me.OutputFile)
        Dim outstr = context.GetValue(Me.InStrings)
        Dim outptuAppend = context.GetValue(Me.Append)
        Dim asyncExecute = New asyncOutputFileDelegate(AddressOf OutputFileInPrivate)
        context.UserState = asyncExecute

        Return asyncExecute.BeginInvoke(outFile, outstr, outptuAppend, callback, state)
    End Function

    Protected Overrides Sub EndExecute(context As System.Activities.AsyncCodeActivityContext, result As System.IAsyncResult)
        Dim asyncExecute = TryCast(context.UserState, asyncOutputFileDelegate)
        Dim outResult = asyncExecute.EndInvoke(result)

        context.SetValue(Me.Result, outResult)
    End Sub

    Private Function OutputFileInPrivate(ByVal target As String, ByVal value As Object, ByVal append As Boolean) As Boolean
        Dim result = False
        Try
            My.Computer.FileSystem.WriteAllText(target, value.ToString, append)
            result = True
        Catch ex As Exception

        End Try
        Return result
    End Function


    Public Sub Register() Implements System.Activities.Presentation.Metadata.IRegisterMetadata.Register
        Dim build As New AttributeTableBuilder
        'OutputFileActivity Activity
        build.AddCustomAttributes(GetType(OutputFileActivity),
                                              New ToolboxBitmapAttribute(GetType(OutputFileActivity), "OutputFile.png"))
        build.AddCustomAttributes(GetType(OutputFileActivity), "InStrings",
                                              New RequiredArgumentAttribute,
                                              New CategoryAttribute(My.Resources.OutputFileCategory),
                                              New DisplayNameAttribute(My.Resources.OutputFileAppendDisplay),
                                              New DescriptionAttribute(My.Resources.OutputFileAppendDescription))
        build.AddCustomAttributes(GetType(OutputFileActivity), "OutputFile",
                                              New RequiredArgumentAttribute,
                                              New EditorAttribute(GetType(FilepathPropertyEditor), GetType(DialogPropertyValueEditor)),
                                              New CategoryAttribute(My.Resources.OutputFileCategory),
                                              New DisplayNameAttribute(My.Resources.OutputFileOutputFileDisplay),
                                              New DescriptionAttribute(My.Resources.OutputFileOutputFileDescription))
        build.AddCustomAttributes(GetType(OutputFileActivity), "Append",
                                              New RequiredArgumentAttribute,
                                              New EditorAttribute(GetType(InArgumentBoolPropertyEditor), GetType(PropertyValueEditor)),
                                              New CategoryAttribute(My.Resources.OutputFileCategory),
                                              New DisplayNameAttribute(My.Resources.OutputFileAppendDisplay),
                                              New DescriptionAttribute(My.Resources.OutputFileAppendDescription))
        build.AddCustomAttributes(GetType(OutputFileActivity), "Result",
                                              New CategoryAttribute(My.Resources.OutputFileCategory),
                                              New DisplayNameAttribute(My.Resources.OutputFileResultDisplay),
                                              New DescriptionAttribute(My.Resources.OutputFileResultDescription))

        MetadataStore.AddAttributeTable(build.CreateTable)
    End Sub

End Class
